/**
 * 
 */
package com.shiro.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shiro.mapper.UserMapper;
import com.shiro.pojo.User;
import com.shiro.pojo.UserExample;
import com.shiro.service.UserRoleService;
import com.shiro.service.UserService;

/**
 * @author zyj
 *
 */
@Service
public class UserServiceIml implements UserService {
	
	@Autowired 
	private UserMapper userMapper;
    @Autowired 
    private UserRoleService userRoleService;

	@Override
	public String getPassword(String name) {
		User user = getByName(name);
		if(null == user) {
			return null;
		}
		return user.getPassword();
	}

	@Override
	public User getByName(String name) {
		UserExample example = new UserExample();
		example.createCriteria().andNameEqualTo(name);
		List<User> users = userMapper.selectByExample(example);
		if(users.isEmpty()) {
			return null;
		}
		return users.get(0);
	}

	@Override
	public List<User> list() {
		UserExample example = new UserExample();
		example.setOrderByClause("id desc");
		return userMapper.selectByExample(example);
	}

	@Override
	public void add(User user) {
		userMapper.insert(user);

	}

	@Override
	public void delete(Long id) {
		userMapper.deleteByPrimaryKey(id);
        userRoleService.deleteByUser(id);
	}

	@Override
	public User get(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(User user) {
		userMapper.updateByPrimaryKey(user);
	}

}
